/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "3d_object.h"

namespace tri{
object_3d* object_3d::load3dsMesh(int& m_count,const char* p_filename){
	FILE* l_file;          //file pointer
	int i = 0;
	unsigned short chunk_id;
	unsigned int chunk_lenght;
	unsigned short l_qty;
	object_3d* meshlist = NULL;
	m_count = 0;
	
	if ((l_file = fopen(p_filename,"rb"))==NULL){
		return NULL;
	}
	
	int obj_count = 0;
	//count the ammount of object in file
	while((ftell (l_file) < filelength( fileno( l_file)))) // loop throught hole file
	{
		fread(&chunk_id,2,1,l_file);
		fread(&chunk_lenght,4,1,l_file);
		switch(chunk_id){
		case 0x4d4d:break;
		case 0x3d3d:break;
		case 0x4000:
			obj_count++;
			fseek(l_file,chunk_lenght-6,SEEK_CUR);
			break;
		default:
			fseek(l_file,chunk_lenght-6,SEEK_CUR);
		}
	}
	fseek(l_file,0,SEEK_SET);
//	printf("OBJECT COUNT: %i\n",obj_count);
    	std::string name;
    	m_count = obj_count;
    	meshlist = new object_3d[m_count];
    	int m_index = -1;
 
    	object_3d* cur_mesh = NULL;
    	while((ftell (l_file) < filelength( fileno( l_file)))) // loop throught hole file
	{
		//getche() // for debugging reasons
		fread(&chunk_id,2,1,l_file);
//		printf("CHUNK ID: %x\n",chunk_id);
		fread(&chunk_lenght,4,1,l_file);
//		printf("CHUNK Lenght: %i\n",chunk_lenght);
		switch(chunk_id){
		case 0x4d4d:break;
		case 0x3d3d:break;
		case 0x4000:
			m_index++;
			name = "";
			cur_mesh = &meshlist[m_index];
			i = 0;
			unsigned char c;
			do {
				fread(&c,1,1,l_file);
				if(c!='\0'){
					name.append(1,c);
				}
				++i;
			}while(c!='\0' && i < 50);
			cur_mesh->setName(name);
			//printf("Object: %s\n",name.c_str());
			break;
		case 0x4100:break;
		case 0x4110:
			fread(&l_qty,sizeof(unsigned short),1,l_file);
			//printf("Vertex Count: %i\n",l_qty);
			cur_mesh->mVertexBuffer.allocate(l_qty);
					
			float v[3];
			for (i=0;i<l_qty;i++){
				fread(&v[0],sizeof(float),3,l_file);
				cur_mesh->mVertexBuffer.assign(i,&v[0]);
				//printf("Vertex : %f %f %f\n",v[0],v[1],v[2]);
			}
			break;
		case 0x4120:
			fread(&l_qty,sizeof(unsigned short),1,l_file);
			//printf("Polygon Count: %i\n",l_qty);
			cur_mesh->mFaceBuffer.allocate(l_qty);
			unsigned short f[3],u;
			for (i=0;i<l_qty;i++){
				fread(f,sizeof(unsigned short),3,l_file);
				cur_mesh->mFaceBuffer.assign(i,(GLushort)f[0],(GLushort)f[1],(GLushort)f[2]);
//				printf("Polygon : %i %i %i\n",f[0],f[1],f[2]);
				fread(&u,sizeof(unsigned short),1,l_file);
			}
			break;
		case 0x4140:
			fread(&l_qty,sizeof(unsigned short),1,l_file);
			//printf("TexC Count: %i\n",l_qty);
			cur_mesh->mTexCBuffer.allocate(l_qty);
			float t[2];
			for (i=0;i<l_qty;i++){
				fread(t,sizeof(float),2,l_file);
				cur_mesh->mTexCBuffer.assign(i,t[0],1-t[1]);
				//printf("Tex : %f %f\n",t[0],t[1]);
			}
			break;
		default:
			fseek(l_file,chunk_lenght-6,SEEK_CUR);
		}
	}
    printf("Done...");
	fclose(l_file);
	return meshlist;
}
}


